/**
 *荣誉资质证书管理js
 */

var form;
var E;
$(function () {

    layui.use(['form', 'laydate', 'table', 'upload'], function () {
        var table = layui.table;
        form = layui.form;

        var $ = layui.jquery
            , upload = layui.upload;

        tableIns = table.render({
            id: 'id',
            elem: '#credentialList',
            url: '/credential/getCredentials',
            method: 'get', //默认：get请求
            title: '荣誉资质列表',
            cellMinWidth: 80,
            page: false,
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 0, //成功的状态码，默认：0
                countName: 'total', //数据总数的字段名称，默认：count
                dataName: 'datas' //数据列表的字段名称，默认：data
            },
            cols: [[
                {
                    field: 'photo_url', title: '资质证书', align: 'center',
                    templet: function (d) {
                        return '<div class="credentialImg">' +
                            '<img src=' + d.photo_url + '></div>'
                    }
                }
                , {fixed: 'right', title: '操作', align: 'center', toolbar: '#optBar', width: '25%'}
            ]],
            done: function (res, curr, count) {
                /*使fixed列自适应单元格高度*/
                $(".layui-table-main  tr").each(function (index, val) {
                    $($(".layui-table-fixed .layui-table-body tbody tr")[index]).height($(val).height());
                    $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                });
                layer.photos({
                    photos: '.credentialImg',
                    anim: 3
                })
            }
        });


        //监听工具条
        table.on('tool(classicTable)', function (obj) {
            var data = obj.data;

            if (obj.event === 'del') {
                // 删除
                deleteClassic(data);
            }

        });


        var form = layui.form
            , layer = layui.layer;
        var credentialImg;

        //封面图片上传
        var uploadInst = upload.render({
            elem: '#credentialImg'
            , url: '/file/imgUpload'
            , before: function (obj) {

            }
            , done: function (res) {

                //如果上传失败
                if (res.code < 0) {
                    return layer.msg('上传失败');

                } else {
                    layer.msg('图片上传成功');
                    credentialImg = res.url;
                    //上传成功
                    $('#credentialPhoto').attr('src', res.url); //照片回显
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#credentialText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(formCredential)', function (data) {

            //获取表单值

            var img = credentialImg; //图片url

            $.ajax({
                type: "POST",
                url: "/credential/admin/addCredential",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    photo_url: img
                }),
                datatype: 'json',
                error: function (data) {
                    layer.alert("上传失败");
                    // layer.closeAll();
                },
                success: function (res) {

                    if (res.code == 0) {
                        layer.alert("上传成功");
                        $('#credentialPhoto').attr('src', ""); //照片回显
                        // layer.closeAll();
                    } else {
                        layer.alert("上传失败");
                        // layer.closeAll();
                    }
                }
            });

            return false;
        });

    });

});


function deleteClassic(data) {
    //删除
    if (null != data.classicId) {
        layer.confirm('您确定要删除吗？', {
            btn: ['确认', '取消'] //按钮
        }, function () {
            $.post("/credential/admin/deleteCredential", {"classicId": data.id}, function (data) {
                if (data.code == 0) {
                    layer.alert("删除成功", function () {
                        layer.closeAll();
                        load(data); //刷新
                    });
                } else {
                    layer.alert("删除失败");
                }
            });
        }, function () {
            layer.closeAll();
        });
    }
}






